﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Sinacor.Infra.Service.Data;
using Sinacor.Infra.Service.Security.Authorization;
using Sinacor.Servico.Bovespa.Carteira.BusinessEntity;

namespace Sinacor.Servico.Bovespa.Carteira.ResourceAccess
{
    public class Carteira : BaseResourceAccess
    {
        public Carteira()
            : base(AuthorizationContext.Current.CompanyId)
        {
        }

        public BusinessEntity.Carteira ConsultarMenorCarteira(BusinessEntity.Parameter.ConsultarMenorCarteira parameter)
        {
            BusinessEntity.Carteira response = null;
            StringBuilder sql = new StringBuilder();
            DbCommand dbcommand;
            IDataReader reader;

            sql.Append(@"SELECT TBOCART.CD_CARLIQ ""CodigoCarteira"", ");
            sql.Append(@"TBOCART.CD_CARCUS ""CodigoCarteiraCustodia"", ");
            sql.Append(@"TBOCART.CD_CARRJ ""CodigoCarteiraRJ"", ");
            sql.Append(@"TBOCART.DS_CART ""DescricaoCarteira"", ");
            sql.Append(@"TBOCART.IN_TXCUS ""IndicadorTaxaCustodia"", ");
            sql.Append(@"TBOCART.IN_INTCUS ""IndicadorIntegraCustodia"" ");
            sql.Append(@"FROM TBOMERCAR ");
            sql.Append(@"        INNER JOIN TBOCART ON TBOCART.CD_CARLIQ=TBOMERCAR.CD_CARLIQ ");
            sql.Append(@"WHERE TBOMERCAR.CD_ALMERC = :P_CD_ALMERC ");
            sql.Append(@"        AND TBOMERCAR.CD_NATOPE = :P_CD_NATOPE ");
            sql.Append(@"        AND TBOMERCAR.CD_BOLSA = :P_CD_BOLSA ");
            sql.Append(@"        AND TBOMERCAR.NR_PRIORIDADE=(SELECT MIN(X.NR_PRIORIDADE) ");
            sql.Append(@"									 FROM TBOMERCAR X ");
            sql.Append(@"									 WHERE X.CD_ALMERC=TBOMERCAR.CD_ALMERC ");
            sql.Append(@"									   AND X.CD_NATOPE=TBOMERCAR.CD_NATOPE ");
            sql.Append(@"									   AND X.CD_BOLSA=TBOMERCAR.CD_BOLSA) ");

            dbcommand = this.DataBase.GetSqlStringCommand(sql.ToString());

            this.DataBase.AddInParameter(dbcommand, "P_CD_ALMERC", DbType.String, Sinacor.Servico.Bovespa.BovespaBase.Util.Funcoes.ToUpper(Sinacor.Infra.Service.Common.EnumConverter.ToString<Sinacor.Servico.Bovespa.Carteira.BusinessEntity.Enum.TipoMercado?>(parameter.CodigoMercado)));
            //Alterado de char para TipoNaturezaOperacao
            //this.DataBase.AddInParameter(dbcommand, "P_CD_NATOPE", DbType.String, parameter.NaturezaOperacao);
            this.DataBase.AddInParameter(dbcommand, "P_CD_NATOPE", DbType.AnsiString, Sinacor.Servico.Bovespa.BovespaBase.Util.Funcoes.ToUpper(Sinacor.Infra.Service.Common.EnumConverter.ToString<Sinacor.Servico.Bovespa.Carteira.BusinessEntity.Enum.TipoNaturezaOperacao?>(parameter.NaturezaOperacao)));
            this.DataBase.AddInParameter(dbcommand, "P_CD_BOLSA", DbType.String, parameter.CodigoBolsa);
                                                                                      
            reader = this.DataBase.ExecuteReader(dbcommand);

            if (reader.Read())
            {
                response = new BusinessEntity.Carteira();
                response.CodigoCarteira = Convert.ToInt32(reader["CodigoCarteira"]);
                response.CodigoCarteiraCustodia =  Convert.ToInt32(reader["CodigoCarteiraCustodia"]);

                if (reader["CodigoCarteiraRJ"] == DBNull.Value)
                    response.CodigoCarteiraRJ = ' ';
                else
                    response.CodigoCarteiraRJ = Convert.ToChar(reader["CodigoCarteiraRJ"]);

                response.DescricaoCarteira = reader["DescricaoCarteira"].ToString();
                response.IndicadorTaxaCustodia = Convert.ToChar(reader["IndicadorTaxaCustodia"]);
                response.IndicadorIntegraCustodia = Convert.ToChar(reader["IndicadorIntegraCustodia"]);
            }

            return response;
        }

        public bool ValidarCarteiraMercado(BusinessEntity.Parameter.ValidarCarteiraMercado parameter)
        {
            object ret;
            DbCommand dbcommand;
            StringBuilder sql = new StringBuilder();

            sql.Append("SELECT  1 ");
            sql.Append("FROM TBOMERCAR ");
            sql.Append("WHERE TBOMERCAR.CD_ALMERC = :P_CD_ALMERC ");
            sql.Append("        AND TBOMERCAR.CD_NATOPE = :P_CD_NATOPE ");
            sql.Append("        AND TBOMERCAR.CD_BOLSA = :P_CD_BOLSA ");
            sql.Append("        AND TBOMERCAR.CD_CARLIQ = :P_CD_CARLIQ ");

            dbcommand = this.DataBase.GetSqlStringCommand(sql.ToString());
                                                                                  
            this.DataBase.AddInParameter(dbcommand, "P_CD_ALMERC", DbType.String,Sinacor.Servico.Bovespa.BovespaBase.Util.Funcoes.ToUpper(Sinacor.Infra.Service.Common.EnumConverter.ToString<Sinacor.Servico.Bovespa.Carteira.BusinessEntity.Enum.TipoMercado?>(parameter.CodigoMercado)));
            //this.DataBase.AddInParameter(dbcommand, "P_CD_NATOPE", DbType.String, parameter.NaturezaOperacao);
            this.DataBase.AddInParameter(dbcommand, "P_CD_NATOPE", DbType.AnsiString, Sinacor.Servico.Bovespa.BovespaBase.Util.Funcoes.ToUpper(Sinacor.Infra.Service.Common.EnumConverter.ToString<Sinacor.Servico.Bovespa.Carteira.BusinessEntity.Enum.TipoNaturezaOperacao?>(parameter.NaturezaOperacao)));
            this.DataBase.AddInParameter(dbcommand, "P_CD_BOLSA", DbType.String, parameter.CodigoBolsa);
            this.DataBase.AddInParameter(dbcommand, "P_CD_CARLIQ", DbType.Int32, parameter.CodigoCarteira);

            ret = this.DataBase.ExecuteScalar(dbcommand);

            return ret != null;
        }
    }
}
